package com.example.crm.servlet;

import com.example.crm.pojo.Customer;
import com.example.crm.service.CustomerService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/customer/list")
public class CustomerListServlet extends HttpServlet {

    CustomerService customerService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

        customerService = (CustomerService) applicationContext.getBean("customerService");

        List<Customer> list = customerService.findAll();

        req.setAttribute("list", list);

        req.getRequestDispatcher("/WEB-INF/jsp/customer/list.jsp").forward(req, resp);
    }
}
